<template>
  <div class="app-container">
    <el-card class="box-card">
      <template #header>
        <div class="clearfix">
          <span>{{ isEdit ? '编辑学生' : '新增学生' }}</span>
        </div>
      </template>

      <el-form
        ref="studentFormRef"
        :model="form"
        :rules="rules"
        label-width="100px"
        style="max-width: 600px"
      >
        <el-form-item label="学生姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入学生姓名" />
        </el-form-item>
        
        <el-form-item label="学号" prop="studentNumber">
          <el-input v-model="form.studentNumber" placeholder="请输入学号" />
        </el-form-item>
        
        <el-form-item label="班级" prop="classId">
          <el-select v-model="form.classId" placeholder="请选择班级" clearable>
            <el-option
              v-for="item in classOptions"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        
        <el-form-item label="专业" prop="majorId">
          <el-select v-model="form.majorId" placeholder="请选择专业" clearable>
            <el-option
              v-for="item in majorOptions"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        
        <el-form-item label="院系" prop="departmentId">
          <el-select v-model="form.departmentId" placeholder="请选择院系" clearable>
            <el-option
              v-for="item in departmentOptions"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        
        <el-form-item label="入学年份" prop="enrollmentYear">
          <el-date-picker
            v-model="form.enrollmentYear"
            type="year"
            placeholder="请选择入学年份"
            format="YYYY"
            value-format="YYYY"
          />
        </el-form-item>
        
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio :label="1">在读</el-radio>
            <el-radio :label="0">毕业</el-radio>
          </el-radio-group>
        </el-form-item>
        
        <el-form-item>
          <el-button type="primary" @click="submitForm">保存</el-button>
          <el-button @click="goBack">返回</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { getStudentById, createStudent, updateStudent } from '@/api/edu'

// 路由相关
const route = useRoute()
const router = useRouter()

// 表单引用
const studentFormRef = ref()

// 是否为编辑模式
const isEdit = ref(false)

// 班级选项
const classOptions = ref([])

// 专业选项
const majorOptions = ref([])

// 院系选项
const departmentOptions = ref([])

// 表单数据
const form = reactive({
  id: undefined,
  name: '',
  studentNumber: '',
  classId: '',
  majorId: '',
  departmentId: '',
  enrollmentYear: '',
  status: 1
})

// 表单验证规则
const rules = {
  name: [{ required: true, message: '请输入学生姓名', trigger: 'blur' }],
  studentNumber: [{ required: true, message: '请输入学号', trigger: 'blur' }],
  classId: [{ required: true, message: '请选择班级', trigger: 'blur' }],
  majorId: [{ required: true, message: '请选择专业', trigger: 'blur' }],
  departmentId: [{ required: true, message: '请选择院系', trigger: 'blur' }],
  enrollmentYear: [{ required: true, message: '请选择入学年份', trigger: 'blur' }]
}

// 获取选项数据
const getOptionsData = async () => {
  // 这里需要调用相应的API获取选项数据
  // 暂时使用模拟数据
  classOptions.value = [
    { id: 1, name: '一班' },
    { id: 2, name: '二班' }
  ]
  
  majorOptions.value = [
    { id: 1, name: '计算机科学与技术' },
    { id: 2, name: '软件工程' }
  ]
  
  departmentOptions.value = [
    { id: 1, name: '计算机学院' },
    { id: 2, name: '软件学院' }
  ]
}

// 获取学生详情
const getStudentDetail = async (id) => {
  try {
    const response = await getStudentById(id)
    Object.assign(form, response.data)
  } catch (error) {
    ElMessage.error('获取学生信息失败')
  }
}

// 提交表单
const submitForm = async () => {
  if (!studentFormRef.value) return
  
  await studentFormRef.value.validate((valid) => {
    if (valid) {
      if (isEdit.value) {
        // 编辑模式
        updateStudent(form)
          .then(() => {
            ElMessage.success('保存成功')
            goBack()
          })
          .catch(() => {
            ElMessage.error('保存失败')
          })
      } else {
        // 新增模式
        createStudent(form)
          .then(() => {
            ElMessage.success('保存成功')
            goBack()
          })
          .catch(() => {
            ElMessage.error('保存失败')
          })
      }
    }
  })
}

// 返回上一页
const goBack = () => {
  router.push('/edu/students')
}

// 初始化
onMounted(() => {
  getOptionsData()
  
  // 判断是否为编辑模式
  if (route.params.id) {
    isEdit.value = true
    getStudentDetail(route.params.id)
  }
})
</script>

<style scoped>
.app-container {
  padding: 20px;
}
</style>